package com.ghyf.mplay.view;

import java.io.File;
import java.util.ArrayList;
import java.util.ListIterator;

import com.ghyf.mplay.util.CommonUtil;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.widget.ImageView;

/**
 * 显示图片的控件
 * @author 陈小红
 *
 */
public class MPicShow extends ImageView {
	private int module_type; // 模块类型
	
	private int zOrder; // 模块的ZOrder
	
	private String file_version;	//文件版本
	
	private String module_name;	//控件名
	
	private int module_uid;//控件标识
	
	private int module_gid;//控件组标识
	

	private int show_interval;
	private ArrayList<File> pic_list; // 图片文件列表
	private MPicShow mPicShow; // 自身实例的引用
	private Thread myThread;
	private Bitmap bm;

	public MPicShow getmPicShow() {
		return mPicShow;
	}

	public void setmPicShow(MPicShow mPicShow) {
		this.mPicShow = mPicShow;
	}

	private boolean runFlag = true;
	
	
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// 切换图片
			mPicShow.setImageBitmap(bm);
		};
	};

	public MPicShow(Context context) {
		super(context);
	}

	/**
	 * 图片切换的线程
	 */
	private Runnable myRunnable = new Runnable() {
		public void run() {
			ListIterator<File> listIterator = pic_list.listIterator();
			while (runFlag) {
				if (!listIterator.hasNext()) { // 没有下一项，找到第一项
					while (listIterator.hasPrevious()) {
						listIterator.previous();
					}
				}
				File item = listIterator.next();
				
				try {
					bm = CommonUtil.fitSizeImg(item.getPath());
					handler.sendEmptyMessage(0);
					Thread.sleep(show_interval * 1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}


			}
		}
	};

	@Override
	/**
	 * 当控件显示时自动执行的方法
	 */
	protected void onAttachedToWindow() {
	
		
		myThread = new Thread(myRunnable);
		runFlag = true;
		myThread.start();

		super.onAttachedToWindow();
	}

	@Override
	/**
	 * 当控件从界面退出时自动执行的方法
	 */
	protected void onDetachedFromWindow() {
		runFlag = false;
		myThread.interrupt();
		super.onDetachedFromWindow();
	}

	public int getShow_interval() {
		return show_interval;
	}

	public void setShow_interval(int show_interval) {
		this.show_interval = show_interval;
	}

	public ArrayList<File> getPic_list() {
		return pic_list;
	}

	public void setPic_list(ArrayList<File> pic_list) {
		this.pic_list = pic_list;
	}
	
	public int getModule_type() {
		return module_type;
	}

	public void setModule_type(int module_type) {
		this.module_type = module_type;
	}

	public int getzOrder() {
		return zOrder;
	}

	public void setzOrder(int zOrder) {
		this.zOrder = zOrder;
	}

	public String getFile_version() {
		return file_version;
	}

	public void setFile_version(String file_version) {
		this.file_version = file_version;
	}

	public String getModule_name() {
		return module_name;
	}

	public void setModule_name(String module_name) {
		this.module_name = module_name;
	}

	public int getModule_uid() {
		return module_uid;
	}

	public void setModule_uid(int module_uid) {
		this.module_uid = module_uid;
	}

	public int getModule_gid() {
		return module_gid;
	}

	public void setModule_gid(int module_gid) {
		this.module_gid = module_gid;
	}
	
	
}
